/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kaiwukj.android.ufamily.app.impl;

import android.content.Context;
import android.net.ParseException;

import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.kaiwukj.android.ufamily.mvp.http.api.ApiRespCode;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.HttpErrorEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.TokenInvalidEvent;
import com.kaiwukj.android.ufamily.mvp.http.exception.ApiException;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.exceptions.CompositeException;
import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener;
import retrofit2.HttpException;

/**
 * Copyright © KaiWu Technology Company
 *
 * @author Haife
 * @job Android Development
 * @company KW | 开物科技
 * @time 2019/7/15
 * @desc 展示 {@link ResponseErrorListener} 的用法
 */
public class ResponseErrorListenerImpl implements ResponseErrorListener {

    public static final String TAG = "ResponseErrorListener";

    @Override
    public void handleResponseError(Context context, Throwable t) {
        t.printStackTrace();
        //这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
        //这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
        // EventBus.getDefault().register(context);//发送端不需要注册监听----huiwings
        String msg = "未知错误";
        if (t instanceof UnknownHostException) {
            msg = "网络异常，请检查网络设置";
        } else if (t instanceof SocketTimeoutException) {
            msg = "请求网络超时";
        } else if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            msg = convertStatusCode(httpException);
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            msg = "数据解析错误";
        } else if (t instanceof CompositeException) {
            for (int i = 0; i < ((CompositeException) t).getExceptions().size(); i++) {
                Throwable throwable = ((CompositeException) t).getExceptions().get(i);
                if (throwable instanceof UnknownHostException) {
                    msg = "网络异常，请检查网络设置";
                } else if (throwable instanceof SocketTimeoutException) {
                    msg = "请求网络超时";
                } else if (throwable instanceof HttpException) {
                    HttpException httpException = (HttpException) t;
                    msg = convertStatusCode(httpException);
                } else if (throwable instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
                    msg = "数据解析错误";
                }
            }
        } else if (t instanceof ConnectException) {
            msg = "服务器连接失败";
        } else if (t instanceof ApiException) {
            msg = handleApiException((ApiException) t);
        }
        //if (t instanceof ApiException) {
        //   Timber.d("---------ApiException----------");
        // } else {
        EventBus.getDefault().post(new HttpErrorEvent(msg, t));
        // }

    }

    private String convertStatusCode(HttpException httpException) {
        String msg;
        if (httpException.code() == 500) {
            msg = "服务器发生错误";
        } else if (httpException.code() == 401) {
            msg = "登录未认证";
        } else if (httpException.code() == 404) {
            msg = "请求地址不存在";
        } else if (httpException.code() == 403) {
            msg = "请求被服务器拒绝";
        } else if (httpException.code() == 307) {
            msg = "请求被重定向到其他页面";
        } else {
            msg = httpException.message();
        }
        return msg;
    }

    private String handleApiException(ApiException e) {
        String msg;
        if (e.getErrCode() == ApiRespCode.TOKEN_INVALID) {
            EventBus.getDefault().post(new TokenInvalidEvent());
            msg = "登录信息失效，请重新登录!";
        } else {
            msg = e.getMessage();
        }
        return msg;
    }
}
